﻿using EcoSystem.Models.Office;
using EcoSystem.Office;
using EcoSystem.Office.Enums;
using EcoSystem.Office.Excel;
using EcoSystem.Office.Word;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;

namespace EcoSystem.Controllers
{

    [ApiController]
    [Route("/office")]
    public class OfficeController : Controller
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("/excel")]
        public IActionResult Excel()
        {
            string chars = "张王李刘天金黄明华小玉花涛贵新阳大非晓廖朱燕灵荣焕兴义来恩建国浩胜圣杰";
            var len = chars.Length - 1;
            List<ExportCar> list = new List<ExportCar>();
            Random rand = new Random();
            for (var i = 0; i < 500000; i++)
            {
                list.Add(new ExportCar()
                {
                    Age = rand.Next(18, 54),
                    CarCode = "贵A" + rand.Next(11111, 99999),
                    Gender = rand.Next(1, 100) % 2 == 0 ? GenderEnum.Female : GenderEnum.Male,
                    Name = chars.Substring(rand.Next(0, len), 1) + chars.Substring(rand.Next(0, len), 1) + chars.Substring(rand.Next(0, len), 1),
                    RegisterDate = DateTime.Now.AddDays(rand.Next(-100, 0))
                });
            }

            var opts = new ExcelExportOptions<ExportCar>();
            opts.Caption = "车牌信息";
            opts.AddColumn(x => x.CarCode, "车牌号")
                .AddColumn(x => x.Name, "姓名")
                .AddColumn(x => x.Gender, "性别")
                .AddColumn(x => x.RegisterDate, "登记日期", null, 60)
                .AddColumn(x => x.Age, "年龄");
            using (var stream = ExcelExportService.WriteExcelToStream(list, opts, ExcelFileType.Excel2007))
            {
                return File(stream.ToArray(), "applicaton/ms-excel", "车牌信息.xlsx");
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("template")]
        public IActionResult ExportTemplate()
        {
            //var list = ExcelImportService.ImportData<ExportCar>(
            //    @"C:\Users\HUTAO\Downloads\车牌信息 (1).xls", new ExcelImportOptions()
            //    {
            //        FileType = ExcelFileType.Excel97_2003,
            //        SheetIndex = 0
            //    });
            //return Json(list);
            var opts = new ExcelExportOptions<ExportCar>();
            opts.Caption = "车牌信息";
            opts.AddColumn(x => x.CarCode, "车牌号")
                .AddColumn(x => x.Name, "姓名", new CellValidate()
                {
                    Options = new List<ExcelCellDropdownItem>() {
                        new ExcelCellDropdownItem() {  CellText = "张三",CellValue ="1001"},
                        new ExcelCellDropdownItem() { CellText = "李四",CellValue ="1002" },
                        new ExcelCellDropdownItem() { CellText = "王二",CellValue ="1003" },
                        new ExcelCellDropdownItem() { CellText = "麻子",CellValue ="1004" },
                        new ExcelCellDropdownItem() { CellText = "王五",CellValue ="1005" },
                        new ExcelCellDropdownItem() { CellText = "赵六",CellValue ="1006" }
                    }
                })
                .AddColumn(x => x.Gender, "性别")
                .AddColumn(x => x.RegisterDate, "登记日期", new CellValidate()
                {
                    Options = new List<ExcelCellDropdownItem>() {
                        new ExcelCellDropdownItem() {  CellText = "2020年",CellValue ="2020"},
                        new ExcelCellDropdownItem() { CellText = "2019年",CellValue ="2019" },
                        new ExcelCellDropdownItem() { CellText = "2018年",CellValue ="2018" },
                        new ExcelCellDropdownItem() { CellText = "2017年",CellValue ="2017" },
                        new ExcelCellDropdownItem() { CellText = "2016年",CellValue ="2016" },
                        new ExcelCellDropdownItem() { CellText = "2015年",CellValue ="2015" }
                    }
                })
                .AddColumn(x => x.Age, "年龄", new CellValidate() { Min = 0, Max = 100 });

            using (var stream = ExcelExportService.WriteTemplate(opts, ExcelFileType.Excel2007))
            {
                return File(stream.ToArray(), "applicaton/ms-excel", "车牌信息模板.xlsx");
            }
        }

        [HttpGet]
        [Route("test/import")]
        public IActionResult TestImport()
        {
            //IWorkbook book = new XSSFWorkbook(new FileInfo(@"C:\Users\HUTAO\Downloads\车牌信息模板.xlsx"));
            var list = ExcelImportService.ImportData<Dictionary<string, object>>(@"C:\Users\HUTAO\Downloads\车牌信息模板.xlsx", new ExcelImportOptions
            {
                SheetIndex = 0,
                FileType = ExcelFileType.Excel2007,
                IsTemplate = true
            });
            return Json(list);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("import")]
        public IActionResult Import()
        {
            //var list = ExcelImportService.ImportData<ExportCar>(
            //    @"C:\Users\HUTAO\Downloads\车牌信息 (1).xls", new ExcelImportOptions()
            //    {
            //        FileType = ExcelFileType.Excel97_2003,
            //        SheetIndex = 0
            //    });
            //return Json(list);
            var opts = new ExcelImportOptions()
            {
                FileType = ExcelFileType.Excel97_2003,
                SheetIndex = 0
            };
            opts.AddColumn("CarCode", typeof(string), "车牌号");
            opts.AddColumn("Name", typeof(string), "姓名");
            opts.AddColumn("Gender", typeof(GenderEnum), "性别");
            opts.AddColumn("RegisterDate", typeof(DateTime), "登记日期");
            opts.AddColumn("Age", typeof(int), "年龄");
            var list = ExcelImportService.ImportData<Dictionary<string, object>>(@"C:\Users\HUTAO\Downloads\车牌信息 (1).xls", opts);
            return Json(list);
        }

        [HttpGet]
        [Route("word")]
        public IActionResult CreateWord()
        
        {
            var model = new Dictionary<string, object>();
            model.Add("Title", "会员普通商品购买订单");
            model.Add("OrderNo", "T" + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
            model.Add("Amount", 364.5);
            model.Add("Name", "张三");
            model.Add("CreateDate", DateTime.Now);
            model.Add("Address", "贵州省贵阳市观山湖区德福中心A5栋");
            model.Add("Mobile", "13078332712");
            model.Add("Remarks", "请发顺丰快递");
            model.Add("Image", new ImageField()
            {
                Image = Image.FromFile(@"d:\测试图片\02.png"),
                Options = new ImageOptions() { Width = 240, Height = 160 }
            });

            var goods = new List<Dictionary<string, object>>();
            Random rand = new Random();
            for (var i = 0; i < 5; i++)
            {
                var price = rand.Next(10, 100);
                var number = rand.Next(1, 5);
                var row = new Dictionary<string, object>();
                row.Add("Title", "测试商品" + (i + 1));
                row.Add("Number", number);
                row.Add("SubTotal", price * number);
                goods.Add(row);
            }
            model.Add("Goods", goods);

            string templatePath = @"C:\Users\HUTAO\Desktop\Template.docx";
            using (var stream = WordExportService.ExportForTemplate(templatePath, model, DocSaveFormat.Pdf))
            {
                return File(stream.ToArray(), "applicaton/ms-excel", "测试Word.pdf");
            }
            //using (var stream = WordExportService.ExportForTemplate(templatePath, model, DocSaveFormat.Docx))
            //{
            //    return File(stream.ToArray(), "applicaton/ms-excel", "测试Word.docx");
            //}
        }


    }
}
